/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.zakariya.stickyheadersapp.adapter;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;
import org.zakariya.stickyheadersapp.ResourceTable;
import org.zakariya.stickyheadersapp.page.StickyBasePage;
import org.zakariya.stickyheadersapp.page.StickyAboutPage;
import org.zakariya.stickyheadersapp.page.StickyDemosPage;

import java.util.List;
/**
 * TabPageAdapter
 *
 * @since 2021-08-20
 */
public class TabPageAdapter extends PageSliderProvider {
    private static final int STICKY_DEMOS = 0;
    private static final int STICKY_ABOUT = 1;
    private List<String> data;
    private Context context;
    private StickyBasePage tabPage;

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param data 数据源
     */
    public TabPageAdapter(Context context, List<String> data) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        ComponentContainer layout = initTabPage(position);
        if (layout != null) {
            tabPage.init(layout);
            componentContainer.addComponent(layout);
        }
        return layout;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
        componentContainer.removeComponent((Component) object);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return component == object;
    }

    private ComponentContainer initTabPage(int tabPositon) {
        ComponentContainer layout = null;
        switch (tabPositon) {
            case STICKY_DEMOS:
                layout = (ComponentContainer) LayoutScatter.getInstance(context).parse(
                    ResourceTable.Layout_layout_sticky_demos, null, false);
                tabPage = new StickyDemosPage(context);
                break;
            case STICKY_ABOUT:
                layout = (ComponentContainer) LayoutScatter.getInstance(context).parse(
                    ResourceTable.Layout_layout_sticky_about, null, false);
                tabPage = new StickyAboutPage(context);
                break;
            default:
                return null;
        }
        return layout;
    }
}
